package m1;

import com.rabbitmq.client.*;

import java.io.IOException;
import java.util.concurrent.TimeoutException;
public class Consumer {
    public static void main(String[] args)throws IOException, TimeoutException {
        //连接
        ConnectionFactory f = new ConnectionFactory();
        f.setHost("192.168.64.140");
        f.setPort(5672);   //5672
        f.setUsername("admin");
        f.setPassword("admin");
        Connection con = f.newConnection();
        Channel c = con.createChannel();

        //创建队列
        c.queueDeclare("helloworld",false,false,false,null );
        //创建回调对象
        DeliverCallback deliverCallback = (consumerTag, message) -> {
            byte[] a = message.getBody();
            String s = new String(a);
            System.out.println("收到:  " + s);
        };


        CancelCallback cancelCallback = consumerTag -> {};
        //开始接收消息,把消息传递给一个回调对象进行处理
        c.basicConsume("helloworld",true,deliverCallback,cancelCallback);
    }
}
